﻿/********************************************************************************
** 作者： Mick
** 邮箱： zheng_jinfan@126.com
** 主页： http://www.zhengjinfan.cn
** 创始时间：2016-2-17
** 描述：
**      [辅助类]站点公共操作辅助类 
*********************************************************************************/
using System;
using System.Web;
using BestEasyCMS.Framework.Extension;

namespace BestEasyCMS.Framework.Util
{
    /// <summary>
    /// [辅助类]站点公共操作辅助类 
    /// </summary>
    public sealed class WebUtil
    {
        #region 构造实例

        /// <summary>
        /// 私有默认构造函数，禁止用户使用new()创建对象
        /// </summary>
        private WebUtil()
        {
        }

        /// <summary>
        /// 构建类的实例对象
        /// </summary>
        public static WebUtil Instance
        {
            get { return new WebUtil(); }
        }

        #endregion 构造实例

        /// <summary>
        /// 获取的应用程序名称，如“/”或“/website/”
        /// </summary>
        public string AppPath()
        {
            string path = HttpContext.Current.Request.ApplicationPath;
            if (path != null && path.Trim() != "/") //判断路径是否为“/”
            {
                path += "/";
            }
            return path;
        }

        /// <summary>
        /// 获取的应用程序URL址址与端口，如“http://www.google.com”或“http://www.google.com:8082”
        /// </summary>
        /// <returns></returns>
        public string AppUrl()
        {
            return string.Format("http://{0}"
                                 , HttpContext.Current.Request.Url.Authority);
        }

        /// <summary>
        /// 获得指定Url参数的值
        /// </summary>
        /// <param name="strName">Url参数</param>
        /// <returns>Url参数的值</returns>
        public string GetQueryString(string strName)
        {
            return HttpContext.Current.Request.QueryString[strName] ?? string.Empty;
        }

        /// <summary>
        /// 获得指定Url参数的int类型值
        /// </summary>
        /// <param name="strName">Url参数</param>
        /// <param name="defValue">缺省值</param>
        /// <returns>Url参数的int类型值</returns>
        public int GetQueryInt(string strName, int defValue)
        {
            string result = GetQueryString(strName);

            return result.ToInt32(defValue);
        }

        /// <summary>
        /// 获得当前页面客户端的IP
        /// </summary>
        /// <returns>当前页面客户端的IP</returns>
        public string GetIp()
        {
            string result = HttpContext.Current.Request.ServerVariables["HTTP_X_FORWARDED_FOR"];
            if (string.IsNullOrEmpty(result))
            {
                result = HttpContext.Current.Request.ServerVariables["REMOTE_ADDR"];
            }

            if (string.IsNullOrEmpty(result))
            {
                result = HttpContext.Current.Request.UserHostAddress;
            }
            result = result == "::1" ? "127.0.0.1" : result;

            if (string.IsNullOrEmpty(result) || !result.IsIp4())
            {
                return "0.0.0.0";
            }

            return result;
        }

        /// <summary>
        /// 获取当前访问来自的浏览器
        /// </summary>
        public string GetBrowser()
        {
            return HttpContext.Current.Request.Browser.Type;
        }

        /// <summary>
        /// 初始化CheckBoxList中哪些是选中了的 
        /// </summary>
        /// <param name="checkList">CheckBoxList</param>
        /// <param name="selval">选中了的值串例如："0,1,1,2,1"</param>
        /// <param name="separator">值串中使用的分割符例如"0,1,1,2,1"中的逗号</param>
        /// <returns></returns>
        public string SetChecked(System.Web.UI.WebControls.CheckBoxList checkList, string selval, string separator)
        {
            selval = separator + selval + separator; //例如："0,1,1,2,1"->",0,1,1,2,1,"
            for (int i = 0; i < checkList.Items.Count; i++)
            {
                checkList.Items[i].Selected = false;
                string val = separator + checkList.Items[i].Value + separator;
                if (selval.IndexOf(val, StringComparison.Ordinal) != -1)
                {
                    checkList.Items[i].Selected = true;
                    selval = selval.Replace(val, separator); //然后从原来的值串中删除已经选中了的
                    if (selval == separator) //selval的最后一项也被选中的话，此时经过Replace后，只会剩下一个分隔符
                    {
                        selval += separator; //添加一个分隔符
                    }
                }
            }
            selval = selval.Substring(1, selval.Length - 2); //除去前后加的分割符号
            return selval;
        }

        /// <summary>
        /// 得到CheckBoxList中选中了的值
        /// </summary>
        /// <param name="checkList">CheckBoxList</param>
        /// <param name="separator">分割符号</param>
        /// <returns></returns>
        public string GetChecked(System.Web.UI.WebControls.CheckBoxList checkList, string separator)
        {
            string selval = "";
            for (int i = 0; i < checkList.Items.Count; i++)
            {
                if (checkList.Items[i].Selected)
                {
                    selval += checkList.Items[i].Value + separator;
                }
            }
            return selval;
        }

        /// <summary>
        /// 将字符串中的单引号字符编码
        /// </summary>
        /// <param name="word">字符串</param>
        /// <returns>返回编码后的字符串</returns>
        public string HtmlEncode(string word)
        {
            word = word.Replace("'", "&#34;");
            return word;
        }

        /// <summary>
        /// 将字符串中的单引号字符解码
        /// </summary>
        /// <param name="word">字符串</param>
        /// <returns>返回解码后的字符串</returns>
        public string HtmlDecode(string word)
        {
            word = word.Replace("&#34;", "'");
            return word;
        }

        /// <summary>
        /// 获取登录名
        /// </summary>
        /// <returns></returns>
        public string GetLoginName()
        {
            return CookieUtil.Instance.GetCookieValue("LOGINNAME", "LoginName");
        }
        /// <summary>
        /// 获取登录名
        /// </summary>
        /// <returns></returns>
        public string GetLoginName(string cookieName,string key)
        {
            return CookieUtil.Instance.GetCookieValue(cookieName, key);
        }

        #region 根据给出的相对地址获取网站绝对地址
        /// <summary>
        /// 根据给出的相对地址获取网站绝对地址
        /// </summary>
        /// <param name="localPath">相对地址</param>
        /// <returns>绝对地址</returns>
        public string GetWebPath(string localPath)
        {
            string path = HttpContext.Current.Request.ApplicationPath;
            string thisPath;
            string thisLocalPath;
            //如果不是根目录就加上"/" 根目录自己会加"/"
            if (path != "/")
            {
                thisPath = path + "/";
            }
            else
            {
                thisPath = path;
            }
            if (localPath.StartsWith("~/"))
            {
                thisLocalPath = localPath.Substring(2);
            }
            else
            {
                return localPath;
            }
            return thisPath + thisLocalPath;
        }

        #endregion

        #region 获取网站绝对地址
        /// <summary>
        ///  获取网站绝对地址
        /// </summary>
        /// <returns></returns>
        public string GetWebPath()
        {
            string path = HttpContext.Current.Request.ApplicationPath;
            string thisPath;
            //如果不是根目录就加上"/" 根目录自己会加"/"
            if (path != "/")
            {
                thisPath = path + "/";
            }
            else
            {
                thisPath = path;
            }
            return thisPath;
        }
        #endregion

        #region 根据相对路径或绝对路径获取绝对路径
        /// <summary>
        /// 根据相对路径或绝对路径获取绝对路径
        /// </summary>
        /// <param name="localPath">相对路径或绝对路径</param>
        /// <returns>绝对路径</returns>
        public string GetFilePath(string localPath)
        {
            return System.Text.RegularExpressions.Regex.IsMatch(localPath, @"([A-Za-z]):\\([\S]*)") ? localPath : HttpContext.Current.Server.MapPath(localPath);
        }

        #endregion
    }
}